package com.atguigu.crowd.mvc.handler;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atguigu.crowd.entity.Menu;
import com.atguigu.crowd.entity.Project;
import com.atguigu.crowd.entity.Tag;
import com.atguigu.crowd.entity.Type;
import com.atguigu.crowd.service.api.ProjectService;
import com.atguigu.crowd.service.api.TagService;
import com.atguigu.crowd.service.api.TypeService;
import com.atguigu.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;


@Controller
public class ProjectHandler {
	
	@Autowired
	private ProjectService projectService;
	@Autowired
	private TypeService typeService;
	
	@Autowired
	private TagService tagService;
	
	//********************************项目标签模块***************************//
	
	//删除标签节点
	
	@ResponseBody
	@RequestMapping("/project/tag/remove.json")
	public ResultEntity<String> removeProjectTag(@RequestParam("id") Integer id){
		tagService.removeProjectTag(id);
		return ResultEntity.successWithoutData();
	}
	//更新标签节点
	@ResponseBody
	@RequestMapping("/project/tag/update.json")
	public ResultEntity<String> editProjectTag(Tag tag){
		tagService.editProjectTag(tag);
		return ResultEntity.successWithoutData();
	}
	
	//添加标签节点
	@ResponseBody
	@RequestMapping("/project/tag/save.json")
	public ResultEntity<String> addProjectTag(Tag tag){
		
		tagService.addProjectTag(tag);
		return ResultEntity.successWithoutData();
	}
	
	//生成标签树
	@ResponseBody
	@RequestMapping("/project/tag/get/tagTree.json")
	public ResultEntity<Tag> getTagTree(){
		List<Tag> tagList = tagService.getAll();   
		Map<Integer, Tag> map = new HashMap<Integer, Tag>();
		Tag root = null;
		//把menu对象放入map中
		for (Tag tag : tagList) {
			Integer id = tag.getId();
			map.put(id,tag);
		}
		//判断是否有父节点
		for (Tag tag : tagList) {
			Integer pid = tag.getPid();
			if (pid==null) {//没有子节点
				root = tag;
				continue;
			}
			//将子节点放入根节点中
			Tag father = map.get(pid);
			father.getChildren().add(tag);
		}
		return ResultEntity.successWithData(root);
	}
	
	
	
	
	
	//********************************项目分类模块***************************//
	//删除（单条、批量）
	@ResponseBody
	@RequestMapping("/project/type/remove/by/id/array.json")
	public ResultEntity<String> removeType(@RequestBody List<Integer> ids){
		
		typeService.removeType(ids);
		return ResultEntity.successWithoutData();
	}
	
	
	//更新分类
	@ResponseBody
	@RequestMapping("/project/type/edit.json")
	public ResultEntity<String> editProjectType(Type type){
		
		typeService.editProjectType(type);
		return ResultEntity.successWithoutData();
		
	}
	
	//添加分类
	@ResponseBody
	@RequestMapping("/project/type/add.json")
	public ResultEntity<String> addProjectType(Type type){
		
		typeService.addProjectType(type);
		return ResultEntity.successWithoutData();
		
	}
	
	//显示项目分类
	@ResponseBody
	@RequestMapping("/project/get/type/page/info.json")
	public ResultEntity<PageInfo<Type>> getTypePageInfo(
			@RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
			@RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
			@RequestParam(value="keyword", defaultValue="") String keyword) {
	
			// 调用Service方法获取分页数据
			PageInfo<Type> pageInfo = typeService.getTypePageInfo(pageNum, pageSize, keyword);
			// 封装到ResultEntity对象中返回（如果上面的操作抛出异常，交给异常映射机制处理）
			return ResultEntity.successWithData(pageInfo);
		}
	
	//********************************项目模块***************************//
	//查看项目详情
	@ResponseBody
	@RequestMapping("/project/look.json")
	public ResultEntity<Project> getProject(@RequestParam("projectId") Integer projectId){
		
		Project project = projectService.getProject(projectId);
		return ResultEntity.successWithData(project);
	}

	//分页显示项目受检列表
	@ResponseBody 
	@RequestMapping("/project/get/page/info.json")
	public ResultEntity<PageInfo<Project>> getPageInfo(
				@RequestParam(value="pageNum", defaultValue="1") Integer pageNum,
				@RequestParam(value="pageSize", defaultValue="5") Integer pageSize,
				@RequestParam(value="keyword", defaultValue="") String keyword
			) {
		
		// 调用Service方法获取分页数据
		PageInfo<Project> pageInfo = projectService.getPageInfo(pageNum, pageSize, keyword);
		// 封装到ResultEntity对象中返回（如果上面的操作抛出异常，交给异常映射机制处理）
		return ResultEntity.successWithData(pageInfo);
	}
	
}
